package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.FileDao;
import cn.web.tunnelrobot.dto.FileDTO;
import cn.web.tunnelrobot.dto.FileUpdate;
import cn.web.tunnelrobot.pojo.File;
import cn.web.tunnelrobot.service.FileService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.MileageConversion;
import cn.web.tunnelrobot.util.UploadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private FileDao fileDao;
    @Override
    public HashMap<String, Object> uploadFile(FileUpdate fileUpload) {
        HashMap<String,Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        List<File> list = new ArrayList<>();
        for (FileDTO dto:fileUpload.getFileDTOList()) {
            File file = new File();
//            file.setFiCreateBy(1);
            file.setFiCreateDate(new Date());
            file.setFiOldName(dto.getFiOldName());
            file.setFiFilePath(dto.getFiFilePath());
            String mile=String.valueOf(MileageConversion.distance(fileUpload.getFiMileage()));
            file.setFiMileage(mile);
            file.setFiType(fileUpload.getFiType());
            list.add(file);
        }
        int result = fileDao.addFile(list);
        if (result > 0){
            map.put("code",0);
            map.put("msg","添加成功");
        }else{
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateFile(File file) {
        HashMap<String,Object> map = new HashMap<>();

//        file.setFiUpdateBy(1);
        int filme=MileageConversion.distance(file.getFiMileage());
        String mile=String.valueOf(filme);
        file.setFiMileage(mile);
        file.setFiUpdateDate(new Date());
        int result = fileDao.updateFile(file);
        if (result > 0){
            map.put("code",0);
            map.put("msg","修改成功");
        }else{
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getFileList(Integer fiType, String fiMileage, Integer pageNo, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<>();
        String mile=null;
        if(fiMileage!=null){
             mile=String.valueOf(MileageConversion.distance(fiMileage));
        }
        List<File> list = fileDao.getFileList(fiType,mile, ((pageNo - 1) * pageSize), pageSize);
        for (int i = 0; i < list.size(); i++) {
            String c=list.get(i).getFiMileage();
            float v = Float.parseFloat(c);
            String mileage = MileageConversion.mileage(v);
            list.get(i).setFiMileage(mileage);
        }
        if (list.size() > 0){
            map.put("code",0);
            map.put("data",list);
            map.put("count",fileDao.getFileCount(fiType,fiMileage));
        }else {
            map.put("code",-1);
            map.put("msg","暂无列表信息");
        }
        return map;
    }
}
